//
//  ShipListView.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-6-17.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
//
//  战斗UI中的船舰列表

#import "ShipListView.h"
#import "Constants.h"
#import "BattleScene.h"

@implementation ShipListView

@synthesize items = _items;

/**
	创建船舰列表的实例
	@param voArray ListItemVo的NSValue数组
	@param c 颜色值
	@param w 宽
    @param h 高
	@returns 返回实例
 */
+(id) listWithVoArray:(CCArray*)voArray
                color:(ccColor4B)c
                width:(GLfloat)w
               height:(GLfloat)h
{
    return [[[self alloc] initWithVoArray:voArray color:c width:w height:h] autorelease];
}



/**
	初始化船舰列表的实例
	@param voArray ListItemVo的NSValue数组
	@param c 颜色值
	@param w 宽
	@param h 高
	@returns 返回实例
 */
-(id) initWithVoArray:(CCArray*)voArray
                color:(ccColor4B)c
                width:(GLfloat)w
               height:(GLfloat)h

{
    if(self=[super initWithColor:c width:w height:h]) 
    {
        //背景
        CCSprite* bg = [CCSprite spriteWithSpriteFrameName:@"listBG.png"]; 
        bg.anchorPoint = ccp(0, 0);
        bg.scaleY = h;
        [self addChild:bg];

        //创建item的底板
        _itemBG = [CCLayerColor layerWithColor:ccc4(0, 255, 100, 0) width:w height:h - 52];
        _itemBG.position = ccp(0, 24);
        [self addChild:_itemBG];
        
        
        //初始化存储item的数组
        self.items = [CCArray array];
        int index = 0;
        _selectItem = nil;
        //读取数据，创建Item
        for (NSValue *value in voArray) 
        {
            struct ListItemVo vo;
            [value getValue:&vo];
            
            ShipListItem* item = [ShipListItem itemWithIcon:vo.icon 
                                                       name:vo.name 
                                                  maxShield:vo.maxShield 
                                                maxArmoured:vo.maxArmoured 
                                                      maxHp:vo.maxHp
                                                      index:vo.index];
            item.position = ccp(0, index * 69);
            [_itemBG addChild:item];
            //添加到数组
            [_items addObject:item];
            //累加计数
            index++;
        }
        
        //上面的封顶
        _topCover = [ToggleSprite spriteWithActive:@"arrowActive.png" unactive:@"arrowUnActive.png"];
        _topCover.position = ccp(0, h - 26);
        [self addChild:_topCover];
        
        //下面的封顶
        _topCover = [ToggleSprite spriteWithActive:@"arrowActive.png" unactive:@"arrowUnActive.png"];
        _topCover.position = ccp(0, 26);
        _topCover.scaleY = -1;
        [self addChild:_topCover];
        
        self.isTouchEnabled = YES;
    }

    return self;
}

- (CCSprite *)maskedSpriteWithSprite:(CCSprite *)textureSprite maskSprite:(CCSprite *)maskSprite 
{ 
    
    // 1
    CCRenderTexture * rt = [CCRenderTexture renderTextureWithWidth:maskSprite.contentSizeInPixels.width height:maskSprite.contentSizeInPixels.height];
    
    // 2
    maskSprite.position = ccp(maskSprite.contentSize.width/2, maskSprite.contentSize.height/2);
    textureSprite.position = ccp(textureSprite.contentSize.width/2, textureSprite.contentSize.height/2);
    
    // 3
    [maskSprite setBlendFunc:(ccBlendFunc){GL_ONE, GL_ZERO}];
    [textureSprite setBlendFunc:(ccBlendFunc){GL_DST_ALPHA, GL_ZERO}];
    
    // 4
    [rt begin];
    [maskSprite visit];        
    [textureSprite visit];    
    [rt end];
    
    // 5
    CCSprite *retval = [CCSprite spriteWithTexture:rt.sprite.texture];
    retval.flipY = YES;
    return retval;
    
}

- (BOOL)selectSpriteForTouch:(CGPoint)touchLocation 
{
    ShipListItem* newItem = nil;
    for (ShipListItem* item in _items) 
    {
        if (CGRectContainsPoint(item.boundingBox, touchLocation)) 
        { 
            newItem = item;
            break;
        }
    } 
    
    if(newItem != nil)
    {
        //取消之前激活的项
        if(_selectItem)
        {
            [_selectItem selectItem:NO];
        }
        //激活选中的项
        _selectItem = newItem;
        [newItem selectItem:YES];
        [[BattleScene sharedLayer].mainLayer selectShipWithIndex:newItem.index];
        return YES;
    }
    else
    {
        return NO;
    }
}

-(void) registerWithTouchDispatcher
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1
                                              swallowsTouches:YES];
}



-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event
{
    if(_items.count == 0) return NO;
    
    CGPoint touchLocation = [_itemBG convertTouchToNodeSpace:touch];
    if([self selectSpriteForTouch:touchLocation])
    {
        CCLOG(@"触摸开始1");
        return YES;
    }
    else
    {
        return NO;
    }
}

-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
    
    CCLOG(@"触摸中2");
}

-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event
{
    
    CCLOG(@"触摸结束3");
}


- (void) dealloc
{
    if(_items)
    {
        [_items removeAllObjects];
        [_items release];
        _items = nil;
    }
    
	// don't forget to call "super dealloc"
	[super dealloc];
}


@end
